package wnhz.com.business.shareshop.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.groupshop.ui.HelpCenterActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.activity.home.RebateGoodsDetailsActivityS;
import wnhz.com.business.shareshop.activity.home.WebViewHomeActivity;
import wnhz.com.business.shareshop.bean.MyRebateBean;
import wnhz.com.business.shareshop.ui.HelpCenterActivityS;
import wnhz.com.business.shareshop.ui.TiXianActivityS;
import wnhz.com.business.shareshop.ui.TwoWeiMaActivityS;
import wnhz.com.business.uitls.GlideImageLoader;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 我的返利
 */
public class RebateActivity extends MyBaseActivity implements ActionBarClickListener {
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.rebate_banner)
    Banner rebate_banner;
    @BindView(R.id.rebate_title)
    TextView rebate_title;
    @BindView(R.id.rebate_title_two)
    TextView rebate_title_two;
    @BindView(R.id.tv_rewards_detail)
    TextView tvRewardsDetail;  //奖励明细
    @BindView(R.id.tv_cash)
    TextView tvCash;  //兑换
    @BindView(R.id.tv_rulea)
    TextView tvRulea;
    @BindView(R.id.tv_yaoqing)
    TextView tvYaoqing;
    @BindView(R.id.tv_makemoney)
    TextView tv_makemoney;
    @BindView(R.id.tv_money)
    TextView tv_money;

    private MyRebateBean myRebateBean;
    private List<String> bannerlist = new ArrayList<>();
    private List<String> bannerBeanList = new ArrayList<String>();

    @Override
    protected void handleMsg(Message msg) {

    }

    public static Intent createIntent(MyBaseActivity activity) {
        Intent intent = new Intent(activity, RebateActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rebate);
        ButterKnife.bind(this);
        actionbar.setData("我的返利", R.drawable.ic_lefft_white, null, 0, "我的二维码", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        getMyRebate();
    }

    private void getMyRebate() {
        showDialog("获取中...");
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        XUtil.Post(Url.Ucenter_myrebate, map, new MyCallBack<String>() { //获取我的分享
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e("---我的返利:  ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String info = jsonObject.optString("info");
                    if(re.equals("1")){
                        Gson gson = new Gson();
                        myRebateBean = gson.fromJson(result, MyRebateBean.class);
                        rebate_title.setText(myRebateBean.getInfo().getTitle());//分享链接给好友，获取永久返利哦
                        rebate_title_two.setText(myRebateBean.getInfo().getTitle_two());//邀请好友可享受7折优惠
                        tv_makemoney.setText(myRebateBean.getInfo().getWithdrawal()); //可兑换(元)
                        tv_money.setText(myRebateBean.getInfo().getSum()); //累计(元)
                        String bannerUrl = myRebateBean.getInfo().getBanner();//banner图
                        bannerBeanList.add(bannerUrl);
                        initTestBannerlist(bannerBeanList);
                    }else {
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    @OnClick(R.id.tv_rewards_detail)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_rewards_detail:

                break;
        }
    }


    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {
        startActivity(TwoWeiMaActivityS.createIntent(this, myRebateBean.getInfo().getCode()));
    }

    @Override
    public void onEditTextClick() {

    }

    @OnClick({R.id.tv_rulea, R.id.tv_yaoqing,R.id.tv_rewards_detail,R.id.tv_cash})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_rulea://返利规则
                startActivity(HelpCenterActivity.createIntent(this, 1)); //int   type   1-规则  2-帮助中心  3-信用秘籍
                break;
            case R.id.tv_yaoqing://邀请好友
                break;
            case R.id.tv_rewards_detail://奖励金明细
                startActivity(DetailRewardsActivity.createIntent(this));
                break;
            case R.id.tv_cash://去兑换
                startActivity(TiXianActivityS.createIntent(this));
                break;
        }
    }


    //轮播图片列表地址
    private void initTestBannerlist(List<String> bannerBeanList) {
        for (int i = 0; i < bannerBeanList.size(); i++) {
            bannerlist.add(bannerBeanList.get(i));
        }
        initBanner();
    }

    /**
     * banner图
     */
    private void initBanner() {
        rebate_banner.setImageLoader(new GlideImageLoader());
        rebate_banner.setImages(bannerlist);
        rebate_banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if(!TextUtils.isEmpty(bannerBeanList.get(position))&&(bannerBeanList.get(position).equals("0"))){
                    Intent intent = new Intent(getBaseContext(), WebViewHomeActivity.class);
                    intent.putExtra("title","返利");
                    intent.putExtra("url",bannerBeanList.get(position));
                    startActivity(intent);
                }else if(TextUtils.isEmpty(bannerBeanList.get(position))&&(!bannerBeanList.get(position).equals("0"))){
                    startActivity(RebateGoodsDetailsActivityS.createIntent(getBaseContext(), bannerBeanList.get(position), bannerBeanList.get(position), "4"));
                }
            }
        });
        rebate_banner.start();
        bannerlist.clear();
    }
}
